import {reactive} from "vue";
import myAxios from "@/plugins/axios";
import {ApiData, LoginData, RegisterData, UserModel} from "@/interfaces/apiResponse.ts";
import myLocalStore from "@/composables/myLocalStore.ts";

import dayjs from "dayjs";



export default function () {
    const form = reactive({
        phone: '13800000001',
        password: 'admin888',
        password_confirmation:'',
        captcha_code:'',
        captcha_key:'',
        code:''
    })
    const isSuperAdmin = () => {
        const info: UserModel = myLocalStore().get('userInfo');
        if (info) {
            return info.id == 1;
        }
        return false;
    }
    function isLogin(){
        return !!myLocalStore().get('token');
    }
    const login = async (data: any) => {
        try {
            const res = await myAxios.request<ApiData<LoginData>>({
                url: 'auth/login',
                method: 'POST',
                data
            });
            myLocalStore().set('token', res.data.token);
            myLocalStore().set('userInfo', res.data.user);
            //判断是否是管理人员，不同的跳转
            //isSuperAdmin() ? router.push({name: 'admin'}) : router.push({name: 'home'});
            location.reload();

        } catch (error) {

        }
    }
    const is_mine = (u_id:any)=>{
        const info: UserModel = myLocalStore().get('userInfo');
        if (info) {
            return info.id == u_id;
        }
        return false;
    }
    const logout = async () => {
        //后端退出登录
        await myAxios.request({
            url:'logout',
            method:"POST"
        })
        myLocalStore().remove('token');
        myLocalStore().remove('userInfo');
        //router.push({name: 'home'});
        location.reload();
    }
    const getRegisterCode = async (phone:any)=>{
            await myAxios.request<ApiData<{code:string}>>({
                url:'send/code',
                method:'POST',
                data:{
                    phone
                }
            })
        //记录发送的时间
            myLocalStore().set('codeSendTime',dayjs())
    }
    const getRepasswordCode = async (phone:any)=>{
        await myAxios.request<ApiData<{code:string}>>({
            url:'send/repassword_code',
            method:'POST',
            data:{
                phone
            }
        })
        //记录发送的时间
        myLocalStore().set('codeSendTime',dayjs())
    }
    //发送时间的差值
    const diffSendTime = ()=>{
        const time = myLocalStore().get('codeSendTime');
        return time ? 60 - dayjs().diff(time,'second') : -1;
    }
    const register = async (data:any)=>{
        await myAxios.request<ApiData<RegisterData>>({
            url:'auth/register',
            method:'POST',
            data
        });
    }
    const repassword = async (data:any)=>{
        await myAxios.request<ApiData<UserModel>>({
            url:'auth/repassword',
            method:'PUT',
            data
        });
    }

    return {form,login,isSuperAdmin,logout,isLogin,getRegisterCode,
        register,diffSendTime,getRepasswordCode,repassword,is_mine};
}